#include "includes.h"


#ifdef USE_TIMER0
void Timer0Init(uint8_t cnt)
{
	TMOD |= 0x02;									//Timer0 mode 2, 8bits auto-reload Timer
	TCKCON &= ~(BIT1|BIT0);				//1 prescaled
	TCKCON |= (BIT1);							//8 prescaled
	TH0 = -cnt;
	TL0 = TH0;
#ifdef USE_TIMER0_ISR
	IE0 |= BIT1;									//Timer0 overflow interrupt enable
#endif	//USE_TIMER0_ISR
	TCON |= BIT4;									//Run Timer0
}
#endif	//USE_TIMER0


#ifdef USE_TIMER1
void Timer1Init(uint8_t cnt)
{
	TMOD |= 0x20;									//Timer1 mode 2, 8bits auto-reload Timer
	TCKCON &= ~(BIT3|BIT2);				//1 prescaled
	TH1 = -cnt;
	TL1 = TL1;
#ifdef USE_TIMER1_ISR
	IE0 |= BIT3;									//Timer1 overflow interrupt enable
#endif	//USE_TIMER1_ISR
	TCON |= BIT6;									//Run Timer1
}
#endif	//USE_TIMER1


#ifdef USE_TIMER2
void Timer2Init(uint16_t cnt)
{
	T2MOD = 0x00;									//Timer2 mode 0, 16bits auto-reload Timer
	T2CON = 0x00;									//Stop
	TCKCON &= ~(BIT4|BIT5|BIT6);	//1 prescaled
	CRCH = (uint8_t)((-cnt)>>8);
	CRCL = (uint8_t)(-cnt);
	TH2 = CRCH;
	TL2 = CRCL;
#ifdef USE_TIMER2_ISR
	IE0 |= BIT5;									//Timer2 overflow interrupt enable
#endif	//USE_TIMER2_ISR
	T2CON |= 0x90;								//Mode0, enable Timer2
}
#endif	//USE_TIMER2


